<template>
  <div class="chat-container">
    <div class="chat-header">
      <div class="chat-header-avatar">
        <img :src="currentContact.avatar" :alt="currentContact.name" class="avatar">
      </div>
      <div class="chat-header-info">
        <div class="chat-header-name">{{ currentContact.name }}</div>
        <div class="chat-header-status">在线</div>
      </div>
      <div class="chat-header-actions">
        <button class="action-btn">
          <i class="fas fa-phone-alt"></i>
        </button>
        <button class="action-btn">
          <i class="fas fa-video"></i>
        </button>
      </div>
    </div>

    <div class="messages-container" ref="messagesContainer">
      <div
          v-for="message in messages"
          :key="message.id"
          class="message"
          :class="{ 'received': !message.isSent, 'sent': message.isSent }"
      >
        <div class="message-content">{{ message.content }}</div>
        <div class="message-time">{{ formatTime(message.time) }}</div>
      </div>
    </div>

    <div class="message-input">
      <textarea
          v-model="newMessage"
          placeholder="输入消息..."
          @keydown.enter.exact.prevent="sendMessage"
      ></textarea>
      <button class="send-btn" @click="sendMessage">
        <i class="fas fa-paper-plane"></i>
      </button>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    currentContact: {
      type: Object,
      required: true
    },
    messages: {
      type: Array,
      default: () => []
    },
    userId: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      newMessage: ''
    };
  },
  methods: {
    formatTime(date) {
      if (!date) return '';
      return new Date(date).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
    },
    sendMessage() {
      if (this.newMessage.trim()) {
        this.$emit('send-message', this.newMessage);
        this.newMessage = '';
        this.$nextTick(() => {
          this.scrollToBottom();
        });
      }
    },
    scrollToBottom() {
      const container = this.$refs.messagesContainer;
      container.scrollTop = container.scrollHeight;
    }
  },
  mounted() {
    this.scrollToBottom();
  }
};
</script>

<style scoped>
.chat-container {
  flex: 1;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  display: flex;
  flex-direction: column;
  height: calc(100vh - 200px);
}

.chat-header {
  padding: 15px 20px;
  border-bottom: 1px solid #e6e8ec;
  display: flex;
  align-items: center;
}

.chat-header-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  margin-right: 12px;
  overflow: hidden;
}

.chat-header-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.chat-header-info {
  flex: 1;
}

.chat-header-name {
  font-weight: 600;
  font-size: 1.1rem;
}

.chat-header-status {
  font-size: 0.8rem;
  color: #777;
}

.chat-header-actions {
  display: flex;
  gap: 15px;
}

.action-btn {
  background: none;
  border: none;
  color: #666;
  cursor: pointer;
  font-size: 1rem;
}

.messages-container {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
  background: #f0f2f5;
  display: flex;
  flex-direction: column;
}

.message {
  max-width: 70%;
  padding: 12px 16px;
  border-radius: 18px;
  margin-bottom: 15px;
  position: relative;
  word-wrap: break-word;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.message.received {
  background: #fff;
  align-self: flex-start;
  border-bottom-left-radius: 4px;
}

.message.sent {
  background: #d1e7ff;
  align-self: flex-end;
  border-bottom-right-radius: 4px;
}

.message-content {
  word-wrap: break-word;
}

.message-time {
  font-size: 0.75rem;
  color: #999;
  margin-top: 5px;
  text-align: right;
}

.message-input {
  padding: 15px;
  border-top: 1px solid #e6e8ec;
  display: flex;
  align-items: center;
  background: white;
}

.message-input textarea {
  flex: 1;
  padding: 12px 15px;
  border: 1px solid #e6e8ec;
  border-radius: 24px;
  outline: none;
  font-size: 1rem;
  resize: none;
  height: 50px;
  max-height: 150px;
}

.send-btn {
  background: #2f80ed;
  color: white;
  border: none;
  border-radius: 50%;
  width: 40px;
  height: 40px;
  margin-left: 10px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>
